﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class quanhuyenPhuongXaSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public quanhuyenPhuongXaSer()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public List<_listJsontoTree_Maps> getListTree(string[] quanhuyen, string[] phuongxa)
        {
            try
            {
                List<_listJsontoTree_Maps> _listRetuen = new List<_listJsontoTree_Maps>();

                var _listMVT = dbContext.QUAN_HUYEN.Where(p => quanhuyen.Contains(p.MA_QUAN)).ToList();

                foreach (var item in _listMVT)
                {
                    _listJsontoTree_Maps itemMVT = new _listJsontoTree_Maps();
                    itemMVT.id = item.MA_QUAN;
                    itemMVT.text = item.TEN_QUAN;
                    itemMVT.type = "quahuyen";
                    itemMVT.clat = item.CLAT;
                    itemMVT.clong = item.CLONG;

                    var listChild = dbContext.PHUONG_XA.Where(p => p.MA_QUAN == item.MA_QUAN && phuongxa.Contains(p.MA_PHUONG)).ToList();
                    foreach (var itemloaiTb in listChild)
                    {
                        _listJsontoTree_Maps itemLoaiTb = new _listJsontoTree_Maps();
                        itemLoaiTb.id = itemloaiTb.MA_PHUONG.ToString();
                        itemLoaiTb.text = itemloaiTb.TEN_PHUONG;
                        itemLoaiTb.type = "phuongxa";
                        itemLoaiTb.clat = itemloaiTb.CLAT;
                        itemLoaiTb.clong = itemloaiTb.CLONG;
                        itemMVT.children.Add(itemLoaiTb);
                    }
                    _listRetuen.Add(itemMVT);
                }

                return _listRetuen;

            }
            catch (Exception e)
            {
                List<_listJsontoTree_Maps> _listRe = new List<_listJsontoTree_Maps>();
                return _listRe;
            }
        }

        public List<QUAN_HUYEN> getList_QH(string[] quanhuyenByme)
        {
            try
            {
                return dbContext.QUAN_HUYEN.Where(p => quanhuyenByme.Contains(p.MA_QUAN)).ToList();
            }
            catch (Exception e)
            {
                List<QUAN_HUYEN> _listRe = new List<QUAN_HUYEN>();
                return _listRe;
            }
        }

        public List<QUAN_HUYEN> getList_QH_by_To_ql(string toql, string[] phuongxaByeMe)
        {
            try
            {

                var listpx = (from i in dbContext.PHUONG_XA
                        join k in dbContext.TO_PHUONGXA on i.MA_PHUONG equals k.PHUONGXAID
                              where k.TOID == toql && phuongxaByeMe.Contains(i.MA_PHUONG)
                        select i.MA_QUAN).ToList();

                return dbContext.QUAN_HUYEN.Where(p => listpx.Contains(p.MA_QUAN)).ToList();
            }
            catch (Exception e)
            {
                List<QUAN_HUYEN> _listRe = new List<QUAN_HUYEN>();
                return _listRe;
            }
        }

        public QUAN_HUYEN getQHFirstByTo(string toql, string[] phuongxaByeMe)
        {
            try
            {

                var listpx = (from i in dbContext.PHUONG_XA
                              join k in dbContext.TO_PHUONGXA on i.MA_PHUONG equals k.PHUONGXAID
                              where k.TOID == toql && phuongxaByeMe.Contains(i.MA_PHUONG)
                              select i.MA_QUAN).First();

                return dbContext.QUAN_HUYEN.Where(p => listpx == p.MA_QUAN ).First(); ;
            }
            catch (Exception e)
            {
                QUAN_HUYEN _listRe = new QUAN_HUYEN();
                return _listRe;
            }
        }

        public PHUONG_XA getList_PX_First_By_Quan_Huyen(string quanhuyenId, string toq, string[] phuongxaByeMe)
        {
            try
            {
                var listpx = (from i in dbContext.PHUONG_XA
                              join k in dbContext.TO_PHUONGXA on i.MA_PHUONG equals k.PHUONGXAID
                              where (k.TOID == toq || string.IsNullOrEmpty(toq) == true) && phuongxaByeMe.Contains(i.MA_PHUONG) && i.MA_QUAN == quanhuyenId
                              select i).First();
                return listpx;
                //return dbContext.PHUONG_XA.Where(p => p.MA_QUAN == quanhuyenId && phuongxaByeMe.Contains(p.MA_PHUONG)).ToList();
            }
            catch (Exception e)
            {
               PHUONG_XA _listRe = new PHUONG_XA();
                return _listRe;
            }
        }

        public List<PHUONG_XA> getList_PX(string quanhuyenId, string toq, string[] phuongxaByeMe)
        {
            try
            {
                var listpx = (from i in dbContext.PHUONG_XA
                              join k in dbContext.TO_PHUONGXA on i.MA_PHUONG equals k.PHUONGXAID
                              where (k.TOID == toq || string.IsNullOrEmpty(toq) == true) && phuongxaByeMe.Contains(i.MA_PHUONG) && i.MA_QUAN == quanhuyenId
                              select i).ToList();
                return listpx;
                //return dbContext.PHUONG_XA.Where(p => p.MA_QUAN == quanhuyenId && phuongxaByeMe.Contains(p.MA_PHUONG)).ToList();
            }
            catch (Exception e)
            {
                List<PHUONG_XA> _listRe = new List<PHUONG_XA>();
                return _listRe;
            }
        }

        public List<PHUONG_XA> getList_PX_By_ToQL(string tokythuat)
        {
            try
            {
                return (from i in dbContext.PHUONG_XA
                        join k in dbContext.TO_PHUONGXA on i.MA_PHUONG equals k.PHUONGXAID
                        where k.TOID == tokythuat
                        select i).ToList();

            }
            catch (Exception e)
            {
                List<PHUONG_XA> _listReturn = new List<PHUONG_XA>();
                return _listReturn;
            }
        }

        public string[] GetPhuongXaByTo(string[] ToKt)
        {
            try
            {
                return dbContext.TO_PHUONGXA.Where(p => ToKt.Contains(p.TOID)).Select(p => p.PHUONGXAID).ToArray();

            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string[] ListQuanHuyenByPX(string[] phuongxa)
        {
            try
            {
                return dbContext.PHUONG_XA.Where(p => phuongxa.Contains(p.MA_PHUONG)).Select(p => p.MA_QUAN).ToArray();

            }
            catch (Exception e)
            {
                return null;
            }
        }

        public bool AdQuanHuyen(QUAN_HUYEN entity)
        {
            try
            {
                dbContext.QUAN_HUYEN.Add(entity);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public string UpdateQuanHuyen(QUAN_HUYEN entity)
        {
            dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.MA_QUAN;
        }

        public QUAN_HUYEN GetQuanHuyenById(string Id)
        {
            try
            {
                return dbContext.QUAN_HUYEN.Where(p => p.MA_QUAN == Id).First();
            }
            catch (Exception e)
            {
                QUAN_HUYEN _ent = new QUAN_HUYEN();
                return _ent;
            }
        }

        public bool DeteleQuanHUyen(string Id)
        {
            try
            {
                var entity = GetQuanHuyenById(Id);
                dbContext.QUAN_HUYEN.Remove(entity);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception ex) { return false; }
        }

        public bool AdPhuongXa(PHUONG_XA entity)
        {
            try
            {
                dbContext.PHUONG_XA.Add(entity);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public string UpdatePhuongXa(PHUONG_XA entity)
        {
            dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.MA_PHUONG;
        }

        public bool DeletePhuongXa(string Id)
        {
            try
            {
                var entity = GetPhuongXaById(Id);
                dbContext.PHUONG_XA.Remove(entity);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception ex) { return false; }
        }

        public PHUONG_XA GetPhuongXaById(string Id)
        {
            try
            {
                return dbContext.PHUONG_XA.Where(p => p.MA_PHUONG == Id).First();
            }
            catch (Exception e)
            {
                PHUONG_XA _ent = new PHUONG_XA();
                return _ent;
            }
        }

        public List<QUAN_HUYEN> getList_QH_All()
        {
            try
            {
                return dbContext.QUAN_HUYEN.ToList();
            }
            catch (Exception e)
            {
                List<QUAN_HUYEN> _listRe = new List<QUAN_HUYEN>();
                return _listRe;
            }
        }

        public List<PHUONG_XA> getList_PX_By_Quan_All(string quanhuyenId)
        {
            try
            {
                return dbContext.PHUONG_XA.Where(p => p.MA_QUAN == quanhuyenId).ToList();
            }
            catch (Exception e)
            {
                List<PHUONG_XA> _listRe = new List<PHUONG_XA>();
                return _listRe;
            }
        }

        public bool checkPhuongXaOnTo(string tokt, string phuongxa)
        {
            try
            {
                var entity = dbContext.TO_PHUONGXA.Where(p => p.TOID == tokt && p.PHUONGXAID == phuongxa).First();
                if (entity != null || entity.ID != 0)
                    return true;
                else return false;
            }
            catch (Exception ex) { return false; }
        }

        public TO_PHUONGXA getTOPHUONGXA(string tokt, string phuongxa)
        {
            try
            {
                var entity = dbContext.TO_PHUONGXA.Where(p => p.TOID == tokt && p.PHUONGXAID == phuongxa).First();
                return entity;
            }
            catch (Exception ex) { TO_PHUONGXA _ml = new TO_PHUONGXA(); return _ml; }
        }

        public bool DetelePuongXaOnTo(string tokt, string phuongxa)
        {
            try
            {
                var entity = dbContext.TO_PHUONGXA.Where(p => p.TOID == tokt && p.PHUONGXAID == phuongxa).First();
                dbContext.TO_PHUONGXA.Remove(entity);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception ex) { return false; }
        }

    }
}
